package com.zfk.transaction.controller;


import com.alibaba.fastjson.JSON;
import com.zfk.transaction.entity.Product;
import com.zfk.transaction.entity.Seller;
import com.zfk.transaction.entity.User;
import com.zfk.transaction.service.GoodsListService;
import com.zfk.transaction.service.UserService;
import com.zfk.transaction.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "GoodsListController",description = "首页点击频道，进入频道商品界面")
public class GoodsListController {


    @Autowired
    private GoodsListService goodsListService;
    @Autowired
    private UserService userService;

    /**
     * 根据点击的频道position，选择对应的频道
     * @param position
     * @return
     */
    @GetMapping("/channel/{position}")
    @ApiModelProperty("getGoodsList")
    public Response getGoodsList(@PathVariable int position){

        List<Product> result = goodsListService.getByChannelId(position);

        return Response.ok(result);
    }

    /**
     * 根据商品id获得商品信息
     * @param product_id
     * @return
     */
    @GetMapping("/getGoods")
    @ApiModelProperty("getGoods")
    public Response getGoods(@RequestParam String product_id){

        Product product = goodsListService.getById(product_id);
        System.out.println(product);
        return Response.ok(product);
    }

    /**
     * 根据商品id获得发布该商品的用户信息
     * @param product_id
     * @return
     */
    @GetMapping("/getSeller")
    @ApiModelProperty("getGoods")
    public Response getSeller(@RequestParam String product_id){

        Product product = goodsListService.getById(product_id);
        int seller_id = Integer.parseInt(product.getSeller_id());
        Seller seller = userService.getById(seller_id);
        return Response.ok(seller);
    }

    @GetMapping("/getByLikeName")
    @ApiModelProperty("getByLikeName")
    public Response getByLikeName(@RequestParam String name){
        List<Product> list = goodsListService.getByLikeName(name);
        return Response.ok(list);
    }

    @GetMapping("/getByUserId")
    @ApiModelProperty("/getByUserId")
    public Response getByUSerId(@RequestParam String user_id){

        List<Product> result = goodsListService.getByUserId(user_id);
        System.out.println(result);
        return Response.ok(result);
    }

    @GetMapping("/deleteById")
    @ApiModelProperty("/deleteById")
    public Response deleteById(@RequestParam String product_id){

        int result = goodsListService.deleteById(product_id);
        return Response.ok(result);
    }

    @PostMapping("/updatePro")
    @ApiModelProperty("/update")
    public Response update(@RequestBody Map<String,Object> jsonString){

        String string = JSON.toJSONString(jsonString);

        Map<String, Object> map = JSON.parseObject(string, Map.class);

        Product product = new Product((String)map.get("product_id"),(String)map.get("cover_price"),(Integer) map.get("channel_id"),(String)map.get("figure"),(String)map.get("name"),null,null);
        int result = goodsListService.update(product);
        return Response.ok(result);
    }

}
